package com.zeyi.blog.util;

import java.lang.reflect.Field;

/**
 * @author 黎先森
 */
public class CastEmptyToNullUtil {
    public static <T> T cast(T t) {
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(t);
                if (value != null && value.equals("")) {
                    field.set(t, null);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return t;
    }
}
